package com.example.zce.back.controller.sys;

import com.alibaba.fastjson.JSON;
import com.example.zce.library.common.bean.ResultMapBean;
import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.data.sys.entity.BackRole;
import com.example.zce.library.data.sys.entity.BackUser;
import com.example.zce.library.data.sys.model.BackMenuModel;
import com.example.zce.library.data.sys.model.BackPermissionModel;
import com.example.zce.library.data.sys.service.BackMenuService;
import com.example.zce.library.data.sys.service.BackRoleService;
import com.example.zce.library.data.sys.service.BackUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：角色控制器
 *
 * @author zengyz
 * @date 2018/7/27 0:11
 */
@Controller
@RequestMapping("/sys/role")
public class BackRoleController {

    @Autowired
    private BackRoleService backRoleService;

    @Autowired
    private BackMenuService backMenuService;

    /**
     * 首页
     * @return
     */
    @GetMapping("/index.html")
    public ModelAndView indexHtml(){
        return new ModelAndView("sys/role-index");
    }

    /**
     * 添加页面
     * @return
     */
    @GetMapping("/add.html")
    public ModelAndView addHtml(){
        return new ModelAndView("sys/role-add");
    }

    /**
     * 修改页面
     * @param request
     * @return
     */
    @GetMapping("/update.html")
    public ModelAndView updateHtml(HttpServletRequest request){
        String id = request.getParameter("id");
        BackRole backRole = backRoleService.findById(id);
        request.setAttribute("backRole",backRole);
        return new ModelAndView("sys/role-update");
    }

    /**
     * 配置权限页面
     * @param request
     * @return
     */
    @GetMapping("/permission.html")
    public ModelAndView permissionHtml(HttpServletRequest request){
        String id = request.getParameter("id");
        ModelAndView modelAndView=new ModelAndView();
        List<BackPermissionModel> backPermissionModels = backMenuService.getPermissionTree(id);
        modelAndView.addObject("zNodes",JSON.toJSONString(backPermissionModels));
        modelAndView.addObject("roleId",id);
        modelAndView.setViewName("sys/role-permission");
        return modelAndView;
    }

    /**
     * 配置权限提交
     * @param ids
     * @param roleId
     * @return
     */
    @ResponseBody
    @PostMapping("/permission.json")
    public ResultMapBean permissionJson(String ids,String roleId){
        try {
            if (StringUtils.isNotEmpty(ids)){
                ids=ids.substring(0,ids.length()-1);
            }
            backRoleService.insertRoleMenu(roleId,ids.split(","));
            return ResultMapBean.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }

    /**
     * 分页数据
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/list.json")
    public ResultTableBean<BackRole> listJson(HttpServletRequest request){
        Map params=new HashMap();
        params.put("pageNum",request.getParameter("page"));
        params.put("pageSize",request.getParameter("limit"));

        String name = request.getParameter("name");
        if (StringUtils.isNotEmpty(name)){
            params.put("name","%"+name+"%");
        }

        String sTime = request.getParameter("sTime");
        if (StringUtils.isNotEmpty(sTime)){
            params.put("sTime",sTime);
        }

        String eTime = request.getParameter("eTime");
        if (StringUtils.isNotEmpty(eTime)){
            params.put("eTime",eTime);
        }

        ResultTableBean<BackRole> resultTableBean = backRoleService.findByParams(params);
        return resultTableBean;
    }

    /**
     * 保存和修改
     * @param backRole
     * @return
     */
    @ResponseBody
    @PostMapping("/saveupdate.json")
    public ResultMapBean saveupdateJson(BackRole backRole){
        try {
            int i = backRoleService.saveOrUpdate(backRole);
            if (i!=0){
                return ResultMapBean.ok();
            }else{
                return ResultMapBean.error(501,"操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/delete.json")
    public ResultMapBean deleteJson(String id){
        try {
            int i = backRoleService.delete(id);
            if (i!=0){
                return ResultMapBean.ok();
            }else{
                return ResultMapBean.error(501,"操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }

}
